package com.dormitory_springboot.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.dormitory_springboot.entity.Dormitory;
import com.dormitory_springboot.entity.Maintenance;
import com.dormitory_springboot.entity.Student;
import com.dormitory_springboot.form.SearchForm;
import com.dormitory_springboot.mapper.BuildingMapper;
import com.dormitory_springboot.mapper.DormitoryMapper;
import com.dormitory_springboot.mapper.MaintenanceMapper;
import com.dormitory_springboot.mapper.StudentMapper;
import com.dormitory_springboot.service.MaintenanceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dormitory_springboot.vo.MaintenanceVO;
import com.dormitory_springboot.vo.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author admin
 * @since 2024-01-09
 */
@Service
public class MaintenanceServiceImpl extends ServiceImpl<MaintenanceMapper, Maintenance> implements MaintenanceService {
    @Autowired
    private MaintenanceMapper maintenanceMapper;
    @Autowired
    private DormitoryMapper dormitoryMapper;
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private BuildingMapper buildingMapper;


    @Override
    public PageVO list(Integer page, Integer size) {
        Page<Maintenance> maintenancePage = new Page<>(page, size);
        Page<Maintenance> resultPage = maintenanceMapper.selectPage(maintenancePage, null);
        List<Maintenance> maintenanceList = resultPage.getRecords();
        List<MaintenanceVO> maintenanceVOList = new ArrayList<>();
        for (Maintenance maintenance : maintenanceList) {
            MaintenanceVO maintenanceVO = new MaintenanceVO();
            BeanUtils.copyProperties(maintenance, maintenanceVO);
            // 进行关联搜索
            Dormitory dormitory = dormitoryMapper.selectById(maintenance.getDormitoryId());
            Student student = studentMapper.selectById(maintenance.getApplicantStudentId());
            // 设置相应字段
            maintenanceVO.setDormitoryName(dormitory.getName());
            maintenanceVO.setApplicantStudentName(student.getName());
            maintenanceVO.setApplicantStudentTelephone(student.getContact());
            maintenanceVOList.add(maintenanceVO);
        }
        PageVO pageVO = new PageVO();
        pageVO.setData(maintenanceVOList);
        pageVO.setTotal(resultPage.getTotal());
        return pageVO;
    }

    @Override
    public PageVO search(SearchForm searchForm) {
        Page<Maintenance> maintenancePage = new Page<>(searchForm.getPage(), searchForm.getSize());
        Page<Maintenance> resultPage;
        if (searchForm.getValue().isEmpty()) {
            resultPage = this.maintenanceMapper.selectPage(maintenancePage, null);
        } else {
            QueryWrapper<Maintenance> queryWrapper = new QueryWrapper<>();
            queryWrapper.like(searchForm.getKey(), searchForm.getValue());
            resultPage = this.maintenanceMapper.selectPage(maintenancePage, queryWrapper);
        }
// 想法？
//        // 判断搜索条件是否为空
//        if (!searchForm.getValue().isEmpty()) {
//            // 根据不同的搜索字段设置不同的查询条件
//            if ("request_id".equals(searchForm.getKey())) {
//                queryWrapper.like("request_id", searchForm.getValue());
//            } else if ("dormitory_number".equals(searchForm.getKey())) {
//                queryWrapper.like("dormitory_number", searchForm.getValue());
//            } else if ("status".equals(searchForm.getKey())) {
//                queryWrapper.like("status", searchForm.getValue());
//            }
//            // 添加其他字段的条件...
//        }
//
//        // 添加关联表的查询条件
//        queryWrapper.inSql("dormitory_id", "SELECT id FROM dormitory WHERE your_condition_here");
//
//        Page<Maintenance> resultPage = maintenanceMapper.selectPage(maintenancePage, queryWrapper);


        List<MaintenanceVO> maintenanceVOList = new ArrayList<>();

        for (Maintenance maintenance : resultPage.getRecords()) {
            MaintenanceVO maintenanceVO = new MaintenanceVO();
            BeanUtils.copyProperties(maintenance, maintenanceVO);
            // 进行关联搜索
            Dormitory dormitory = dormitoryMapper.selectById(maintenance.getDormitoryId());
            Student student = studentMapper.selectById(maintenance.getApplicantStudentId());
            // 设置相应字段
            maintenanceVO.setDormitoryName(dormitory.getName());
            maintenanceVO.setApplicantStudentId(student.getId());
            maintenanceVO.setApplicantStudentName(student.getName());
            maintenanceVO.setApplicantStudentTelephone(student.getContact());
            maintenanceVOList.add(maintenanceVO);
        }
        PageVO pageVO = new PageVO();
        pageVO.setData(maintenanceVOList);
        pageVO.setTotal(resultPage.getTotal());
        return pageVO;
    }

    @Override
    public PageVO listByStudentId(Integer studentId, Integer page, Integer size) {
        // 创建一个分页对象，指定当前页码和每页大小
        Page<Maintenance> maintenancePage = new Page<>(page, size);
        // 构造查询条件，使用LambdaQueryWrapper指定查询条件
        LambdaQueryWrapper<Maintenance> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(Maintenance::getApplicantStudentId, studentId);
        // 按照创建日期降序排序
        queryWrapper.orderByDesc(Maintenance::getStartTime);
        // 调用MyBatis Plus的selectPage方法，通过maintenanceMapper按学生ID查询分页数据
        Page<Maintenance> resultPage = this.maintenanceMapper.selectPage(maintenancePage, queryWrapper);
        // 创建一个用于存放转换后的VO对象的列表
        List<MaintenanceVO> maintenanceVOList = new ArrayList<>();
        // 遍历查询结果的每一条记录
        for (Maintenance maintenance : resultPage.getRecords()) {
            // 创建一个VO对象，用于存放转换后的数据
            MaintenanceVO maintenanceVO = new MaintenanceVO();
            // 使用Spring的BeanUtils工具类，将Maintenance对象的属性拷贝到MaintenanceVO对象中
            BeanUtils.copyProperties(maintenance, maintenanceVO);
            // 根据maintenance的dormitoryId查询对应的宿舍名称，并设置到VO对象中
            maintenanceVO.setBuildingName(this.buildingMapper.selectById(maintenance.getBuildingId()).getName());
            maintenanceVO.setDormitoryName(this.dormitoryMapper.selectById(maintenance.getDormitoryId()).getName());
            maintenanceVO.setDormitoryId(this.dormitoryMapper.selectById(maintenance.getDormitoryId()).getId());
            // 根据maintenance的studentId查询对应的学生名称，并设置到VO对象中
            maintenanceVO.setApplicantStudentName(this.studentMapper.selectById(maintenance.getApplicantStudentId()).getName());
            maintenanceVO.setApplicantStudentId(studentId);
            maintenanceVO.setApplicantStudentTelephone(this.studentMapper.selectById(maintenance.getApplicantStudentId()).getContact());
            // 将VO对象添加到列表中
            maintenanceVOList.add(maintenanceVO);
        }
        // 创建PageVO对象，用于存放最终的分页结果
        PageVO pageVO = new PageVO();
        // 设置总记录数
        pageVO.setTotal(resultPage.getTotal());
        // 设置转换后的VO列表
        pageVO.setData(maintenanceVOList);
        // 返回最终的PageVO对象
        return pageVO;
    }

//    @Override
//    public MaintenanceDetailsDTO findDetailsById(Integer id) {
//        Maintenance maintenance = maintenanceMapper.selectById(id);
//        Dormitory dormitory = dormitoryMapper.selectById(maintenance.getDormitoryId());
//        Student applicant = studentMapper.selectById(maintenance.getApplicantStudentId());
//        MaintenanceDetailsDTO detailsDTO = new MaintenanceDetailsDTO(maintenance, dormitory, applicant);
//        return detailsDTO;
//    }



}
